package com.ct.ecommerce.base.controller;


import com.ct.ecommerce.api.base.client.DelayMessageService;
import com.ct.ecommerce.api.base.dto.DelayMessageDTO;
import com.ct.ecommerce.base.vo.DelayMessageVO;
import com.ct.ecommerce.framework.core.BeanCopierUtils;
import com.ct.ecommerce.framework.core.response.R;
import com.ct.ecommerce.framework.exception.CustomerException;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author changtong
 * @since 2021/6/29
 */
@RestController
@RequestMapping("/base/delay/message")
@Slf4j
public class DelayMessageSendController {

    @Resource
    private DelayMessageService delayMessageService;

    @PostMapping("")
    @PreAuthorize("hasAuthority('message:send')")
    public R<String> send(@RequestBody DelayMessageVO message) {
        try {
            DelayMessageDTO msg = BeanCopierUtils
                .copyProperties(message, DelayMessageDTO.class);
            delayMessageService.sendDelay(msg,message.getStartSendTime());
            return R.ok("成功...");
        } catch (CustomerException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("发送消息出错 : ", e);
            return R.fail(e.getMessage());
        }
    }


}
